<?php
class AppController extends Controller {
	function beforeFilter() {
		$user=array();
	/*
	 * this code below is used to get userid and user role from user logged in
	 *
	 */
	$Drupal_session_name = "SESS" . md5("greypath.com");
	if(isset($_COOKIE[$Drupal_session_name])) {
			//echo "<br />" . $Drupal_session_name;
		$cvalue = $_COOKIE[$Drupal_session_name];
			//echo "<br/>" . $cvalue;
		$Drupal_db = mysql_connect("localhost","root","password");
		$db_prefix = '';
		mysql_select_db ( "greyway_drupal" , $Drupal_db );
		$q = mysql_query("SELECT uid FROM {$db_prefix}sessions WHERE sid='$cvalue'");
		$row = mysql_fetch_array($q);
		if(($row['uid'] != "") and ($row['uid'] != 0)){
				//echo "<br/>UID+" . $row['uid'];
				// Get user ID
			$suid = $row['uid'];
			$user['id'] =$suid;
				//echo $suid;
			$q = mysql_query("SELECT name, mail FROM {$db_prefix}users WHERE uid='$suid'");
				//echo "<br/>" . $q . mysql_error();
			$Drupalvett = mysql_fetch_array($q);
				//$_COOKIE['LYCEUMU'] = $Drupalvett['name'];
				//$_COOKIE['LYCEUMM'] = $Drupalvett['mail'];
			$userName = $Drupalvett['name'];
			$user['username'] =$userName;
			$email = $Drupalvett['mail'];
			$sRolesQuery = "SELECT name FROM {$db_prefix}role WHERE rid in (select rid from users_roles as ur WHERE ur.uid ='$suid')";
				//echo "<br/>Query=" . $sRolesQuery;
			$roles = mysql_query($sRolesQuery);
			while ($eachRow = mysql_fetch_row($roles))	{
				$userRoles[] = $eachRow[0];
			}
			} else	{
				$userName = "";
				$user['id']="";
				$email = "";
				//exit;
			}
		}
		if($userName){
			$_SESSION['userID']=$userName; //set user name for gallery pages
			//check if user has an admin role
			if(is_array($userRoles)){
				//print_r ($userRoles);
				$arr = $userRoles;
				foreach ($arr as $value){
					if($value == 'Lyceum Administrator'){
						$user['role']="admin";
					}
				}
			}
		}
		if (($user['id']==""))
        {
            // Force the user to login
            $this->redirect(array('action'=>'../../../greypath/'), null, true);
            exit();
        }
		$this->set('user', $user);
		return $user;
	}
}
?>